﻿using System.Windows;
using System.Xml;
using Method = WeiBeeCommon.Core.Method;
using OAuthSina = WeiBeeCommon.Core.OAuthSina;
using Utility = WeiBeeCommon.Core.Utility;
using System.ComponentModel;
using System.Collections.Generic;
using WeiBeeCommon.Core;

namespace Pages
{
    /// <summary>
    /// Interaction logic for SinaUser.xaml
    /// </summary>
    public partial class SinaUser : Window
    {
        public SinaUser()
        {
            InitializeComponent();
        }

        BackgroundWorker _bgwAuthLink;
        OAuthSina oAuth;
        EntityHelper entityHelper = new EntityHelper();
        string _user;
        string _pwd;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                oAuth = new OAuthSina();
                entities = entityHelper.LoadEntities();
                if (entities.Count > 0)
                {
                    currentEntity = 0;
                    oAuth.ConsumerKey = entities[currentEntity].ConsumerKey;
                    oAuth.ConsumerSecret = entities[currentEntity].ConsumerSecret;
                }

                _bgwAuthLink = new BackgroundWorker();
                _bgwAuthLink.DoWork += new DoWorkEventHandler(_bgwAuthLink_DoWork);
                _bgwAuthLink.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_bgwAuthLink_RunWorkerCompleted);
                _user = tbUsername.Text;
                _pwd = tbPassword.Password;
                _bgwAuthLink.RunWorkerAsync();
            }
            catch
            {
                MessageBox.Show("用户登录失败");
                return;
            }
        }

        string authLink;
        void _bgwAuthLink_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((entities.Count > 0) && (currentEntity < entities.Count))
            {
                entities[currentEntity].Token = oAuth.Token;
                entities[currentEntity].TokenSecret = oAuth.TokenSecret;
                currentEntity++;
                entityHelper.SaveEntities(entities);
                if (currentEntity < entities.Count)
                {
                    oAuth = new OAuthSina();
                    oAuth.ConsumerKey = entities[currentEntity].ConsumerKey;
                    oAuth.ConsumerSecret = entities[currentEntity].ConsumerSecret;
                    _bgwAuthLink.RunWorkerAsync();
                }
                else
                {
                    this.Close();
                }
            }
            else if (entities.Count == 0)
            {
                entities.Add(new AuthEntity());
                entities[currentEntity].ConsumerKey = oAuth.ConsumerKey;
                entities[currentEntity].ConsumerSecret = oAuth.ConsumerSecret;
                entities[currentEntity].Token = oAuth.Token;
                entities[currentEntity].TokenSecret = oAuth.TokenSecret;
                entityHelper.SaveEntities(entities);
            }
            else
            {
                this.Close();
            }
        }

        void _bgwAuthLink_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                authLink = oAuth.AuthorizationLinkGet();
                authLink += "&userId=" + _user + "&passwd=" + _pwd + "&oauth_callback=xml";

                string html = Utility.WebRequest(Method.POST, authLink, null);
                oAuth.AccessTokenGet(oAuth.Token, ParseHtml(html));
            }
            catch
            {
                oAuth.Token = string.Empty;
                oAuth.TokenSecret = string.Empty;
            }
        }

        private string ParseHtml(string html)
        {
            XmlDocument xmldocument = new XmlDocument();
            xmldocument.LoadXml(html);
            return xmldocument.GetElementsByTagName("oauth_verifier")[0].InnerText;
        }

        #region MutliAppKey
        List<AuthEntity> entities;
        int currentEntity;
        #endregion MultiAppKey
    }
}
